﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myTravian
{
    public partial class traGridView : UserControl
    {
        protected DataTable dt = null;
        protected int gridViewIndex = 0;
        /// <summary>
        /// 目前紀錄指標在那
        /// </summary>
        public int GridViewIndex
        {
            get
            {
                //+1 是因為紀錄從0開始...
                return gridViewIndex + 1;
            }           
        }
        public traGridView()
        {
            InitializeComponent();
            
        }

        /// <summary>
        /// 取得目前總筆數
        /// </summary>
        public int GridviewRowCount
        {
            get
            {
                return (dt is DataTable)? dt.Rows.Count : gridViewIndex;
            }       
        }
       
        /// <summary>
        /// 把資料填入到GridView 中
        /// </summary>
        /// <param name="p_dt"></param>
        public void SetData(DataTable DT)
        {
            dt = DT;
            dataGridView1.DataSource = dt.DefaultView;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;  
            dataGridView1.Refresh();            
        }

        /// <summary>
        /// 取回目前停留的那一筆。
        /// </summary>
        /// <returns></returns>
        public DataRow GetCurrentRow()
        {
            DataRow row = null;
            if (dt == null) { return null; }
            if ((dt.Rows.Count > 0) & (dt.Rows.Count - 1 >= GridViewIndex))
            {
                //回傳目前那一筆，並把紀錄指標移往下一個
                dataGridView1.CurrentCell = dataGridView1.Rows[GridViewIndex].Cells[0];
                row = dt.Rows[GridViewIndex];
                gridViewIndex++;
            }
            else
            {
                gridViewIndex = 0;
            }
            return row;
        }
    }   
}
